<?php
require_once("BaseDAO.php");
require_once("../Entities/Question.php");

class QuestionDAO extends BaseDAO
{
	function CreateQuestion($question)
	{
		$sql = "insert into Questions(Content ,CategoryId, Sound, ParagraphId) values ('" . 
					$question->Content . "','" . $question->Category . "','" . 
					$question->Sound . "','" . $question->Paragraph . "');";
		mysql_query($sql);
		$question->Id = mysql_insert_id();
		return $question;
	}
	
	function UpdateQuestion($question)
	{
		$sql = "update Questions set Content='" . $question->Content . "',CategoryId='" . 
				$question->Category . "',Sound='" . $question->Sound . "' where Id='" . $question->Id . "';";
		mysql_query($sql);
	}
	
	function DeleteQuestion($id)
	{
		$sql = "delete from Questions where Id='" . $id . "';";
		mysql_query($sql);
	}
	
	function GetQuestionById($id)
	{
		$sql = "select * from Questions where Id='" . $id . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$question = new Question();
			$question->Id = $row["Id"];
			$question->Content = $row["Content"];
			$question->Category = $row["CategoryId"];
			$question->Sound = $row["Sound"];
			$question->Paragraph = $row["ParagraphId"];
			return $question;
		}
		return null;
	}
	
	function GetQuestions($page, $pageSize, &$totalPage, $categoryId)
	{
		if($categoryId == 0)
		{
			$sqlTotalPage = "select * from Questions";
			$sql = "select * from Questions limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
		}
		else
		{
			$sqlTotalPage = "select * from Questions where CategoryId='" . $categoryId . "';";
			$sql = "select * from Questions where CategoryId='" . $categoryId ."' limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
		}
		$resultTotalPage = mysql_query($sqlTotalPage);
		$result = mysql_query($sql);
		$totalPage = ceil(mysql_num_rows($resultTotalPage) / $pageSize);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$question = new Question();
			$question->Id = $row["Id"];
			$question->Content = $row["Content"];
			$question->Category = $row["CategoryId"];
			$question->Sound = $row["Sound"];
			$question->Paragraph = $row["ParagraphId"];
			$list[] = $question;
		}
		return $list;
	}
	
	function GetQuestionsByCategoryId($categoryId)
	{
		if($categoryId == 0)
		{
			$sql = "select * from Questions";
		}
		else
		{
			$sql = "select * from Questions where CategoryId='" . $categoryId . "';";
		}
		$result = mysql_query($sql);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$question = new Question();
			$question->Id = $row["Id"];
			$question->Content = $row["Content"];
			$question->Category = $row["CategoryId"];
			$question->Sound = $row["Sound"];
			$question->Paragraph = $row["ParagraphId"];
			$list[] = $question;
		}
		return $list;
	}
	
	function GetQuestionsByParagraphId($paragraphId)
	{
		$sql = "select * from Questions where ParagraphId='" . $paragraphId . "';";
		$result = mysql_query($sql);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$question = new Question();
			$question->Id = $row["Id"];
			$question->Content = $row["Content"];
			$question->Category = $row["CategoryId"];
			$question->Sound = $row["Sound"];
			$question->Paragraph = $row["ParagraphId"];
			$list[] = $question;
		}
		return $list;
	}
}

?>